<?php


class SimpletextProblemUpdateWidget extends CWidget {

    public $problem;

    public function run(){
        $assetpath = dirname(__FILE__).DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'assets';
        $assets = Yii::app()->getAssetManager()->publish($assetpath, false, -1, true);
        Yii::app()->getClientScript()->registerCssFile($assets.'/style.css');
        $action = isset($_GET['action']) ? $_GET['action'] : '' ;
        switch($action){
            case 'files' :
                $this->updateFiles();
            case 'problems':
                $this->updateProblems();
                break;
            case 'renderviewfile' :
                $this->renderViewFile();
                break;
        }
        $this->render('problemupdate', 
            array(
                'problem' => $this->problem,
                'action' => $action,
                'assets' => $assets
                ));
    }

    public function updateProblems(){
        if (isset($_POST['config'])){
            $config = $_POST['config'];
            $problems = array();
            if (isset($config['problems'])){
                $i = 0;
                foreach ($config['problems'] as $p) {
                    $p['id'] = $i++;
                    $problems[] = $p;
                }
            }
            $this->problem->setConfig('problems', $problems);
            $this->problem->save();
        }
    }

    public function updateFiles(){
        $action2 = isset($_GET['action2'])?$_GET['action2']:'';
        switch($action2){
            case 'uploadviewfile' :
                if (isset($_FILES['viewfileupload'])){
                    $uploads_dir = $this->problem->getDirectoryPath().'view/files';
                    foreach ($_FILES["viewfileupload"]["error"] as $key => $error) {
                        if ($error == UPLOAD_ERR_OK) {
                            $tmp_name = $_FILES["viewfileupload"]["tmp_name"][$key];
                            $name = $_FILES["viewfileupload"]["name"][$key];
                            move_uploaded_file($tmp_name, "$uploads_dir/$name");
                        }
                    }
                    $this->problem->save();
                }
                break;
            case 'deleteviewfile' :
                $filename = $_GET['filename'];
                $path = $this->problem->deleteFile('view/files/'.$filename);
                break;
            case 'downloadviewfile' :
                ob_clean();
                $filename = $_GET['filename'];
                $path = $this->problem->getFile('view/files/'.$filename);
                if ($path !== null){
                    header('Content-type: '.CFileHelper::getMimeType($path));
                    header('Content-Disposition: attachment; filename="'.$filename.'"');
                    readfile($path);
                    exit;
                } else {
                    throw new CHttpException(404, "File not found");
                }
                exit;
                break;
        }
    }
    
    public function renderViewFile(){
        ob_clean();
        $filename = $_GET['filename'];
        $filepath = $this->problem->getFile('view/files/'.$filename);
        header('Content-Type: '.CFileHelper::getMimeType($filepath));
        header('Content-Length: '.filesize($filepath));
        header('Content-Disposition: attachment; filename="'.$filename.'"');
        readfile($filepath);
        exit;
    }
}